/* 打开一个标签 */
var currIndex = 0;
var closeIndex = 0;
var i = 0;
var j = 0;
$('#tabs').tabs({
	  onSelect: function(title,index)
	  {
		   var tabsLen = $(".tabs li").length;
			var tabsLeft = "yes";
        $(".tabs li").each(function(index, element) {
			$(this).removeClass("left").removeClass("right");
			if(!$(this).hasClass("tabs-selected") && tabsLeft == "yes")
			{
				$(this).addClass("left");	
			}else if($(this).hasClass("tabs-selected"))
			{
				
				tabsLeft = "no";
				
			}else if(!$(this).hasClass("tabs-selected") && tabsLeft == "no")
			{
				$(this).addClass("right");	
			}
		});
		if(currIndex <= closeIndex && i == 2)
		{
			$('#tabs').tabs('select', currIndex);
			i = 0;
		}else if(currIndex > closeIndex && j == 2)
		{
			$('#tabs').tabs('select', currIndex);
			j = 0;
		}
		var tab = $('#tabs').tabs('getSelected');
		currIndex = $('#tabs').tabs('getTabIndex',tab);
	  },
	  onClose: function(title,index){
		 closeIndex = index;
		if(currIndex == 0 && closeIndex == 0)
		{
			$('#tabs').tabs('select', 0);
		}else if(currIndex == closeIndex)
		{
			$('#tabs').tabs('select', currIndex - 1);
		}else if(currIndex <= closeIndex)
		{			
			--closeIndex;
			++i;
			$('#tabs').tabs('select', currIndex);	
		}else if(currIndex > closeIndex)
		{			
			--closeIndex;
			--currIndex;
			++j;
			$('#tabs').tabs('select', currIndex);	
		}
		if(title === "主界面")
		{
			$("#mainWindowPage").removeClass("open");
			$("#mainWindowPage .tree_li").remove();
		}
	  }
	});
function OpenTab(title, url, icon, isFromOthers) {
	/**    如果这个标题的标签存在，则选择该标签    否则添加一个标签到标签组   isFromOthers表示是否从其他标签页打开此标签页的 */
	
	/*
	@author: luokw
	@date: 2015/11/09
	@desc: 限制打开tabs的数量
	*/	
	var tabcount = $('#tabs').tabs('tabs').length;	
	if ($("#tabs").tabs('exists', title)) {
		$("#tabs").tabs('select', title);
		if(title === "主界面")
		{
			$("#mainWindowPage .tree_li").remove();
			setTimeout(function(){$("#mainWindowPage").addClass("open").append('<div class="tree_li"></div>');},10);	
		}
		if(isFromOthers)
		{
			var fieldServerTab = $("#tabs").tabs('getTab', title);
			$("#tabs").tabs('update', {
				tab:fieldServerTab,
				options: {
					content : createTabContent(url)
				}
			});
		}
	} else {
		if (tabcount <= 5) {
			$("#tabs").tabs('add', {
				title : title,
				content : createTabContent(url),
				closable : true,
				icon : icon
			});
		}else {
			$.messager.alert('警告', '<p>您打开的太多了，请关闭不用的窗口！</p>', 'warning');
			return false;
		}
	}

}
/* 生成标签内容 */
function createTabContent(url) {
	return '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="'
			+ url + '"></iframe>';
}
